package com.atguigu.schedule.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;


import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;


public class JDBCUtil {

    private static DataSource dataSource;

    static {
        try {

                //1. 加载 druid.properties 配置文件
                InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
                Properties prop = new Properties();
                prop.load(is);
                //2. 创建 Druid 连接池对象
                 dataSource =  DruidDataSourceFactory.createDataSource(prop);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /*
3. 提供 获得 连接池对象的方法
 */
    public static DataSource getDataSource(){
        return dataSource;
    }

    /*
    4. 提供 从连接池中 获取连接对象Connection的 方法
     */
    public static Connection getConnetion() throws SQLException {
        Connection conn = dataSource.getConnection();
        return  conn;
    }
}
